package com.zang.netty.io.bio;

import com.zang.netty.io.model.enums.SocketEnum;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * SocketManager Server
 * 同步阻塞式的服务端管理器，负责启动SocketServer线程
 * @author Zhang Qiang
 * @Date 2019/7/18 16:25
 */

public class TimeServer {

    public static void main(String[] args) {
        int port = SocketEnum.PORT.getNum();
        if (args != null && args.length > 0){
            try {
                port = Integer.valueOf(args[0]);
            }catch (NumberFormatException e){

            }
        }
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("The time server is start in port :" + port);
            Socket socket = null;
            int total = 0;
            while (true){
                total++;
                socket = serverSocket.accept();
                new Thread(new TimeServerHandler(socket)).start();
                System.out.println("有新连接，创建线程 ： " + total);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null){
                System.out.println(" the time server close ");
                try {
                    serverSocket.close();
                    serverSocket = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
